package com.iniapp.app.web.action.admin;


import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.commons.lang.StringUtils;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.iniapp.app.web.action.BaseCtrl;
import com.inibb.app.service.admin.IGenericCodeService;
import com.inibb.app.vo.GenericCodeSC;
import com.inibb.app.vo.GenericCodeVO;

@Controller("genericCodeCtrl")
@Scope("session")//page
public class GenericCodeCtrl extends BaseCtrl  implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IGenericCodeService genericCodeService;
	
	private LazyDataModel<GenericCodeVO> codeListModel;
	private String currentType;
	private GenericCodeVO selectedCode;
	private GenericCodeVO editCode = new GenericCodeVO();
	private LazyDataModel<GenericCodeVO> selectedCodes;

	@PostConstruct
	public void init(){
		GenericCodeSC paginationCriteria = new GenericCodeSC();
		paginationCriteria.setStartRow(0);
		paginationCriteria.setPageSize(10);
		codeListModel = searchCodes(paginationCriteria);
		
	}

	private LazyDataModel<GenericCodeVO> searchCodes(final GenericCodeSC paginationCriteria){
		LazyDataModel<GenericCodeVO> clm = new LazyDataModel<GenericCodeVO>() {
			private static final long serialVersionUID = 1L;

			@Override
			public List<GenericCodeVO> load(int rowStart, int pageSize, String arg2, SortOrder sorOder, Map<String, String> arg4) {
//				GenericCodeSC pc = new GenericCodeSC();
				paginationCriteria.setStartRow(rowStart);
				paginationCriteria.setPageSize(pageSize);
				
				return genericCodeService.searchPageCodes(paginationCriteria);
			}
			
			@Override  
		    public GenericCodeVO getRowData(String rowKey) {  
		        //In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data  
//				return genericCodeService.getCode(Integer.parseInt(rowKey));
		        List<GenericCodeVO> cars = (List<GenericCodeVO>) this.getWrappedData();  
		        for(GenericCodeVO car : cars) {  
		            if(car.getCodeId().toString().equals(rowKey))  
		                return car;  
		        }  
		          
		        return null;  
		    }  
		  
		    @Override  
		    public String getRowKey(GenericCodeVO car) {  
		        return car.getCodeId().toString();
		    }  
		};
		int total = genericCodeService.countPageCodes(paginationCriteria);
		clm.setRowCount(total);
		return clm;
	}
	
	public void searchCodesByType(){
		GenericCodeSC paginationCriteria = new GenericCodeSC();
		paginationCriteria.setStartRow(0);
		paginationCriteria.setPageSize(10);
		if(StringUtils.isNotEmpty(currentType))
			paginationCriteria.setParentId(Integer.parseInt(currentType));
		codeListModel = searchCodes(paginationCriteria);
	}
	
	public void createCode(){
		editCode = new GenericCodeVO();
		if(StringUtils.isNotEmpty(currentType))
			editCode.setParentId(Integer.parseInt(currentType));
	}
	
	public void editCode(){
		editCode = getSelectedCode();
	}
	
	public void saveEditCode(){
		genericCodeService.saveOrUpdate(editCode);
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "保存记录成功！",  "保存记录成功！");  
        FacesContext.getCurrentInstance().addMessage("保存记录成功！", message);  
	}
	
	public void deleteSelectedCode(){
		genericCodeService.delete(selectedCode);
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "删除记录成功！",  "删除记录成功！");  
        FacesContext.getCurrentInstance().addMessage("删除记录成功！", message);  
	}

	public String getCurrentType() {
		return currentType;
	}

	public void setCurrentType(String currentType) {
		this.currentType = currentType;
	}

	public LazyDataModel<GenericCodeVO> getCodeListModel() {
		return codeListModel;
	}

	public void setCodeListModel(LazyDataModel<GenericCodeVO> codeListModel) {
		this.codeListModel = codeListModel;
	}

	public GenericCodeVO getSelectedCode() {
		return selectedCode;
	}

	public void setSelectedCode(GenericCodeVO selectedCode) {
		this.selectedCode = selectedCode;
	}

	public LazyDataModel<GenericCodeVO> getSelectedCodes() {
		return selectedCodes;
	}

	public void setSelectedCodes(LazyDataModel<GenericCodeVO> selectedCodes) {
		this.selectedCodes = selectedCodes;
	}

	public GenericCodeVO getEditCode() {
		return editCode;
	}

	public void setEditCode(GenericCodeVO editCode) {
		this.editCode = editCode;
	}
	
}
